package coder.nav.convert;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import coder.nav.model.bo.NavCashQueryBO;
import coder.nav.model.NavCash;
import coder.nav.utils.JwtUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

public class NavCashConvert {

    public static class WrapperBuilder {

        public static LambdaQueryWrapper<NavCash> build(NavCashQueryBO bo, QueryWrapper<NavCash> queryWrapper, Long userId) {
            return NavCashConvert.WrapperBuilder.build(bo, queryWrapper, userId, true);
        }

        public static LambdaQueryWrapper<NavCash> build(NavCashQueryBO bo, QueryWrapper<NavCash> queryWrapper, Long userId, boolean orderByCreateDate) {
            if(Objects.isNull(queryWrapper)) {
                queryWrapper = new QueryWrapper<>();
            }
            return NavCashConvert.WrapperBuilder.buildLambda(bo, queryWrapper.lambda(), userId, orderByCreateDate);
        }

        public static LambdaQueryWrapper<NavCash> buildLambda(NavCashQueryBO bo, LambdaQueryWrapper<NavCash> queryWrapper, Long userId, boolean orderByCreateDate) {
            if(Objects.isNull(userId)) {
                throw new RuntimeException("用户未登录");
            }
            queryWrapper.eq(NavCash::getUserId, userId);
            if(StringUtils.isNotBlank(bo.getTitle())) {
                queryWrapper.like(NavCash::getTitle, "%" + bo.getTitle() + "%");
            }
            if(StringUtils.isNotBlank(bo.getRemark())) {
                queryWrapper.like(NavCash::getRemark, "%" + bo.getRemark() + "%");
            }
            if(CollectionUtil.isNotEmpty(bo.getChannels())) {
                queryWrapper.in(NavCash::getChannel, bo.getChannels());
            }
            if(CollectionUtil.isNotEmpty(bo.getFlags())) {
                queryWrapper.in(NavCash::getFlag, bo.getFlags());
            }
            if(CollectionUtil.isNotEmpty(bo.getTypes())) {
                queryWrapper.in(NavCash::getType, bo.getTypes());
            }
            if(Objects.nonNull(bo.getMinMoney())) {
                queryWrapper.ge(NavCash::getMoney, bo.getMinMoney());
            }
            if(Objects.nonNull(bo.getMaxMoney())) {
                queryWrapper.le(NavCash::getMoney, bo.getMaxMoney());
            }
            if(Objects.nonNull(bo.getYear())) {
                queryWrapper.eq(NavCash::getYear, bo.getYear());
            }
            if(Objects.nonNull(bo.getMonth())) {
                queryWrapper.eq(NavCash::getMonth, bo.getMonth());
            }
            if(Objects.nonNull(bo.getDay())) {
                queryWrapper.eq(NavCash::getDay, bo.getDay());
            }
            if(Objects.nonNull(bo.getCreateDate())) {
                queryWrapper.eq(NavCash::getCreateDate, bo.getCreateDate());
            }
            if(Objects.nonNull(bo.getStartDate())) {
                queryWrapper.ge(NavCash::getCreateDate, DateUtil.beginOfDay(bo.getStartDate()));
            }
            if(Objects.nonNull(bo.getEndDate())) {
                queryWrapper.le(NavCash::getCreateDate, DateUtil.endOfDay(bo.getEndDate()));
            }
            if(orderByCreateDate) {
                queryWrapper.orderByDesc(NavCash::getCreateDate);
                queryWrapper.orderByDesc(NavCash::getId);
            }
            return queryWrapper;
        }

    }

}
